<template>
  <div class="login-box">

    <el-card style="max-width: 480px">
    <template #header>
      <div class="card-header">
        <span>用户登陆</span>
      </div>
    </template>

      <div>

        <el-form>
          <el-form-item label="登陆名称">
            <el-input v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item label="登陆密码">
            <el-input show-password v-model="user.password"></el-input>
          </el-form-item>
        </el-form>

      </div>

    <template #footer>
    <el-button v-looding="loading" type="primary" @click="login">登陆</el-button>
    </template>
  </el-card>
  </div>


</template>
<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus';
import { Api } from '../../script/Api';
// 导入路由器对象
import { useRouter } from 'vue-router'
const router = useRouter();

import { storeToRefs } from 'pinia';
import userstore from '../../store/user'
const {update} = userstore();


  const user = ref({
    username: '',
    password: '',
  });

  const loading = ref(false);
    
  const login = () => {
    loading.value = true;
    Api.post('/auth', user.value, (data) =>{
      if(data.success){
        ElMessage.success(data.message)
        update(() => {
          loading.value = false;
          router.push('/user/Main');
        });
      } else {
        loading.value = false;
        ElMessage.error(data.message);
      }
    });
  };
</script>
    
<style scoped>
  .login-box {
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-image: url('/images/11.jpg');
  background-position: center; /* 添加这一行以实现水平和垂直居中对齐 */ /* 使用 @ 符号作为别名指向 src/assets */
  /* 或者使用相对路径（假设当前组件与图片在同一目录下） */
  }
</style>